<template>
  <div class="inventory-management">
    <div class="page-header">
      <div class="header-content">
        <el-button icon="el-icon-arrow-left" @click="goBack">返回首页</el-button>
        <h2>库存管理</h2>
      </div>
    </div>

    <div class="main-content">
      <!-- 看板区域 -->
      <div class="dashboard">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card shadow="hover" class="dashboard-card">
              <div class="card-content">
                <div class="card-title">现存商品</div>
                <div class="card-value">{{ dashboardData.totalProductNumber || 0 }}</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="dashboard-card">
              <div class="card-content">
                <div class="card-title">库存预警</div>
                <div class="card-value">{{ dashboardData.alertProduct || 0 }}</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="dashboard-card">
              <div class="card-content">
                <div class="card-title">库存总值</div>
                <div class="card-value">¥{{ formatPrice(dashboardData.totalPrice) }}</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="dashboard-card">
              <div class="card-content">
                <div class="card-title">库存总量</div>
                <div class="card-value">{{ dashboardData.totalStock || 0 }}</div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 筛选区域 -->
      <div class="filter-panel">
        <el-select v-model="selectedStatus" placeholder="商品状态" @change="handleStatusChange">
          <el-option label="库存正常" value="processed"></el-option>
          <el-option label="库存预警" value="pending"></el-option>
        </el-select>
      </div>

      <!-- 表格区域 -->
      <div class="table-container">
        <el-table
          :data="inventoryList"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="inventoryId" label="库存编号" width="120" align="center"></el-table-column>
          <el-table-column prop="productCode" label="商品编号" width="120" align="center"></el-table-column>
          <el-table-column prop="productName" label="商品名称" width="150" align="center"></el-table-column>
          <el-table-column prop="categoryName" label="商品分类" width="120" align="center"></el-table-column>
          <el-table-column prop="stock" label="库存数量" width="100" align="center"></el-table-column>
          <el-table-column prop="lowStockThreshold" label="预警阈值" width="100" align="center"></el-table-column>
          <el-table-column prop="price" label="单价" width="120" align="center">
            <template slot-scope="scope">
              ¥{{ scope.row.price }}
            </template>
          </el-table-column>
          <el-table-column prop="totalPrice" label="总价" width="120" align="center">
            <template slot-scope="scope">
              ¥{{ scope.row.totalPrice }}
            </template>
          </el-table-column>
          <el-table-column prop="lastUpdated" label="最后更新时间" width="180" align="center">
            <template slot-scope="scope">
              {{ formatDate(scope.row.lastUpdated) }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 'pending' ? 'warning' : 'success'">
                {{ scope.row.status === 'pending' ? '库存预警' : '库存正常' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页区域 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'InventoryManagement',
  data() {
    return {
      loading: false,
      inventoryList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      selectedStatus: 'pending',
      dashboardData: {
        totalProductNumber: 0,
        alertProduct: 0,
        totalPrice: 0,
        totalStock: 0
      }
    }
  },
  created() {
    this.fetchDashboardData();
    this.fetchInventoryList();
  },
  methods: {
    goBack() {
      this.$emit('close');
    },
    formatPrice(price) {
      if (!price) return '0.00';
      return parseFloat(price).toFixed(2);
    },
    formatDate(date) {
      if (!date) return '';
      return new Date(date).toLocaleString();
    },
    handleStatusChange() {
      this.currentPage = 1;
      this.fetchInventoryList();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchInventoryList();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchInventoryList();
    },
    fetchDashboardData() {
      this.$http.get('/inventory/status')
        .then(response => {
          if (response.code === 200) {
            this.dashboardData = response.data;
          } else {
            this.$message.error(response.message || '获取看板数据失败');
          }
        })
        .catch(error => {
          console.error('获取看板数据失败:', error);
          this.$message.error('获取看板数据失败');
        });
    },
    fetchInventoryList() {
      this.loading = true;
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize,
        status: this.selectedStatus
      };

      this.$http.post('/inventory/list', params)
        .then(response => {
          if (response.code === 200) {
            this.inventoryList = response.data.records || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message || '获取库存列表失败');
          }
          this.loading = false;
        })
        .catch(error => {
          console.error('获取库存列表失败:', error);
          this.$message.error('获取库存列表失败');
          this.loading = false;
        });
    }
  }
}
</script>

<style scoped>
.inventory-management {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  overflow: hidden;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.page-header {
  background-color: #304156;
  padding: 0;
  color: white;
  width: 100%;
  z-index: 10;
  margin: 0;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
}

.header-content {
  display: flex;
  align-items: center;
  padding: 10px 20px;
}

.page-header h2 {
  margin-left: 20px;
  margin-bottom: 0;
  color: white;
}

.page-header .el-button {
  background-color: transparent;
  border-color: white;
  color: white;
}

.main-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 20px;
  padding: 20px;
  height: calc(100% - 50px);
  overflow: hidden;
}

.dashboard {
  margin-bottom: 20px;
}

.dashboard-card {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-content {
  text-align: center;
}

.card-title {
  font-size: 16px;
  color: #606266;
  margin-bottom: 10px;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  color: #304156;
}

.filter-panel {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}

.table-container {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
}
</style> 